package cn.dingdan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dingdan.pojo.Type;
import cn.dingdan.sercice.TypeService;

@Controller
public class TypeController {
	
	@Resource(name="typeServiceImpl")
	private TypeService typeService;
	
	@RequestMapping(value="typeList",method=RequestMethod.GET)
	public String getLists(Model model){
		List<Type> types = typeService.getLists();
		model.addAttribute("types", types);
		return "privider";
	}
	@RequestMapping(value="delType",method=RequestMethod.POST)
	public @ResponseBody Object delType(@RequestParam(value="menuTypeId",required=false) Integer menuTypeId){
		int num =typeService.delType(menuTypeId);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("success", num>0);
		if (num>0) {
			map.put("url", "/privider");
		}
		return map;
	}
	@RequestMapping(value="upda",method=RequestMethod.POST)
	public @ResponseBody Object updaType(String cate,Integer menuTypeId){
		Type type=new Type();
		type.setMenuTypeId(menuTypeId);
		type.setCate(cate);
		int num = typeService.updateType(type);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("success", num>0);
		if (num>0) {
			map.put("url", "/privider");
		}
		return map;
	}
	@RequestMapping(value="modification",method=RequestMethod.POST)
	public @ResponseBody Object modificationType(@RequestParam(value="menuTypeId",required=false) Integer menuTypeId){
		List<Type> types =typeService.getLists();
		Map<String,Object> map = new HashMap<String, Object>();
		for (Type type : types) {
			if (type.getMenuTypeId().equals(menuTypeId) || type.getMenuTypeId()==menuTypeId) {
				map.put("success", type);
			}
		}
		return map;
	}
	
	@RequestMapping(value="addType",method=RequestMethod.POST)
	public @ResponseBody  Object addType(@RequestParam(value="cate",required=false)String cate){
		Map<String, Object> map = new HashMap<String, Object>();
		Type type =new Type();
		if (!(cate!=null) || !"".equals(cate)) {
			type.setCate(cate);
			int num=typeService.addType(type);
			map.put("success", num>0);
			if (num>0) {
				map.put("url", "/privider");
			}
		}
		return map;
	}
}
